import unittest
from page.reception.login_page import LoginPage
from common.functions import browser_operation, backhost
from page.reception.usercenter_page import UserCenterPage
from page.backstage.backstage_login_page import Backstage
from page.backstage.backstage_index_page import BackStageIndex


# 创建订单类
class OrderCase(unittest.TestCase):
    """订单相关测试"""

    @classmethod
    def setUpClass(cls) -> None:
        # 调用functions打开浏览器
        driver = browser_operation()
        cls.login = LoginPage(driver)
        cls.login.get_website(cls.login.login_url)
        cls.center = UserCenterPage(driver)
        cls.backstage = Backstage(driver)
        cls.index = BackStageIndex(driver)

    def test_order_01(self):
        """测试商品订单列表里的订单号与后台订单列表里显示的是否一致"""
        # 输入账号密码
        self.login.input_username('zhang')
        self.login.input_password('123456')
        # 登录
        self.login.btn_login()
        # 点击用户中心
        self.center.user_center()
        # 点击我的订单
        self.center.my_order()
        # 获取最新一条订单号
        number1 = self.center.read_order_id()
        # 打开新的标签页面,进入后台地址
        self.login.new_window(backhost)
        # 切换窗口句柄
        backstage_handle = self.center.driver.window_handles[1]
        self.center.driver.switch_to.window(backstage_handle)
        # 调用后台登录模块方法,进行登录
        self.backstage.implicitly_wait()
        self.backstage.click_X()
        self.backstage.backstage_login()
        self.backstage.input_username('admin')
        self.backstage.input_password('admin123')
        self.backstage.btn_login()
        # 切换到菜单栏frame,点击订单管理
        self.index.change_menu_frame()
        self.index.order_manage()
        # 切换回上一级frame
        self.index.change_parent_frame()
        # 切换到主框架frame
        self.index.change_main_frame()
        # 获取最新一条订单号
        number2 = self.index.order_no1()
        # 断言两个页面的订单号相同
        self.assertEqual(number1, number2)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.login.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
